package com.hitemole.levelObject;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.hitemole.objects.GameObject;

public class ComboBar extends GameObject {
	private int targetWidth;
	private int targetHeight;

	protected int srcWidth;
	protected int srcHeight;
	protected int srcX;
	protected int srcY;

	protected float srcTargetRatioX;
	protected float srcTargetRatioY;

	protected int startPercentX;
	protected int endPercentX;
	protected int startPercentY;
	protected int endPercentY;

	private int clipWidth;
	private int clipHeight;

	protected int clipSrcWidth;
	protected int clipSrcHeight;

	public ComboBar(String name, TextureRegion image, Vector2 position) {
		super(name, image, position);
		// TODO Auto-generated constructor stub
		this.srcX = image.getRegionX();
		this.srcY = image.getRegionY();
		this.srcWidth = image.getRegionWidth();
		this.srcHeight = image.getRegionHeight();
		this.clipSrcWidth = srcWidth;
		this.clipSrcHeight = srcHeight;
		this.startPercentX = 0;
		this.startPercentY = 0;
		this.endPercentX = 0;
		this.endPercentY = 0;
		setTargetDimension(srcWidth, srcHeight);
	}

	public void setTargetDimension(int targetWidth, int targetHeight) {
		this.setTargetWidth(targetWidth);
		this.setTargetHeight(targetHeight);
		setClipWidth(targetWidth);
		setClipHeight(targetHeight);
		srcTargetRatioX = (float) targetWidth / (float) srcWidth;
		srcTargetRatioY = (float) targetHeight / (float) srcHeight;
	}

	public void setStart(int x, int y) {
		startPercentX = x;
		startPercentY = y;
	}

	public void setEnd(int x, int y) {
		endPercentX = x;
		endPercentY = y;
	}

	public int getTargetWidth() {
		return targetWidth;
	}

	public void setTargetWidth(int targetWidth) {
		this.targetWidth = targetWidth;
	}

	public int getTargetHeight() {
		return targetHeight;
	}

	public void setTargetHeight(int targetHeight) {
		this.targetHeight = targetHeight;
	}

	public int getClipWidth() {
		return clipWidth;
	}

	public void setClipWidth(int clipWidth) {
		this.clipWidth = clipWidth;
	}

	public int getClipHeight() {
		return clipHeight;
	}

	public void setClipHeight(int clipHeight) {
		this.clipHeight = clipHeight;
	}

	public void draw(SpriteBatch sp, float xScale, float yScale) {
		clipSrcWidth = (int) (Math.abs(startPercentX - endPercentX) / 100f * srcWidth);
		clipSrcHeight = (int) (Math.abs(startPercentY - endPercentY) / 100f * srcHeight);
		int startX = srcX + (int) ((float) startPercentX / 100f * (float) srcX);
		int startY = srcY + (int) ((float) startPercentY / 100f * (float) srcY);
		clipWidth = (int) (srcTargetRatioX * clipSrcWidth);
		clipHeight = (int) (srcTargetRatioY * clipSrcHeight);
		sp.draw(image.getTexture(), position.x*xScale, position.y*yScale, 0, 0, clipWidth, clipHeight,
				xScale, yScale, 0, startX, startY, clipSrcWidth, clipSrcHeight, false,
				false);
		// sp.draw(image.getTexture(), position.x, position.y, startY, startY,
		// startX, startY);
	}

}
